package com.soft.controller;

import com.soft.pojo.PageBean;
import com.soft.pojo.User;
import com.soft.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruoxi
 * @create 2021-12-27 14:40
 */
@WebServlet("/showAllUserByPage")
public class ShowAllUserByPageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService userService = new UserService();
        PageBean<User> userPg = new PageBean<>();

        int currentPage = 1;
        String currentPage_ = req.getParameter("currentPage");
        if (currentPage_ != null) {
            currentPage = Integer.parseInt(currentPage_);
        }

        int currentCount = 10;

        userPg.setCurrentPage(currentPage);
        userPg.setCurrentCount(currentCount);
        userPg.setTotalCount(userService.findUserCount());
        userPg.setTotalPage((int) Math.ceil(userService.findUserCount() * 1.0 / currentCount));
        userPg.setList(userService.findUserByPage((currentPage - 1) * currentCount, currentCount));

        req.setAttribute("userPg", userPg);
        req.getRequestDispatcher("loginInfo.jsp").forward(req, resp);
    }
}
